﻿using RightControl.IRepository;
using RightControl.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DapperExtensions.MySQLExt;

namespace RightControl.Repository.Product {
    public class ProductImgRepository : BaseRepository<ProductImage>, IProductImgRepository
    {

        /// <summary>
        /// 保存菜单权限配置
        /// </summary>
        /// <param name="entitys">商品图片列表</param>
        /// <returns></returns>
        public int SaveImages(IEnumerable<ProductImage> entitys) {
            var result = 0;
            using (var conn = MySqlHelper.GetConnection()) {
                IDbTransaction transaction = conn.BeginTransaction();
                try {
                    if (entitys != null) {
                        result = conn.InsertBatch<ProductImage>(entitys, transaction);
                    }
                    transaction.Commit();
                }
                catch (Exception) {
                    result = -1;
                    transaction.Rollback();
                }
            }
            return result;
        }
        
        /// <summary>
        /// 更新产品图顺序
        /// </summary>
        /// <param name="entitys"></param>
        /// <returns></returns>
        public int UpdateProductImgSort(IEnumerable<ProductImage> entitys) {
            var result = 0;
            using (var conn = MySqlHelper.GetConnection()) {
                IDbTransaction transaction = conn.BeginTransaction();
                try {
                    if (entitys != null) {
                        result = conn.UpdateByIdBatch<ProductImage>(entitys, "Sort");
                    }
                    transaction.Commit();
                }
                catch (Exception ex) {
                    result = -1;
                    transaction.Rollback();
                }
            }
            return result;
        }

    }
}
